home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / comm2 / alist.lha / src / aldef.e < prev    next >
Text File  |  1995-11-08  |  3KB  |  117 lines

  1. /* ALDef.M */
  2.  
  3. OPT MODULE
  4. OPT EXPORT
  5.  
  6. /* AList definitions and structures file */
  7.  
  8. MODULE 'exec/nodes'
  9. MODULE 'exec/ports'
  10.  
  11. OBJECT almsg      /* AList Message */
  12.    mn:mn
  13.    command:LONG
  14.    field1:LONG
  15.    field2:LONG
  16.    arg:LONG
  17. ENDOBJECT
  18.  
  19.  
  20. /* Commands (ALM = AList Message) */
  21. ENUM  ALM_QUIT,      /* Shut down */
  22.       ALM_SEND,      /* Post a message to a list */
  23.       ALM_ADDR,      /* Parse an address/name & return the results */
  24.       ALM_NOOP       /* Do-nothing command */
  25.  
  26.  
  27. /* For the barnacle_start() and _end() and for use as the replyport for barnacle_put_msg() */
  28. /* Just don't use it in barnacle_put_msg() without first calling barnacle_start()!  (: */
  29. DEF alist_replyport:PTR TO mp
  30.  
  31. /*
  32.  * Easiest way to start up a message port specific for the barnacle_put_msg()
  33.  *   function below.
  34.  * WARNING:  ONLY CALL THIS FUNCTION IF alist_replyport DOES *NOT* POINT TO A VALID PORT!
  35.  *           You should only need to call this once, at the start of your program.
  36.  *           You should also check to ensure (alist_replyport <> NIL) before going on...
  37.  */
  38. PROC barnacle_start()
  39.    alist_replyport := CreateMsgPort()     /* A happy little OS 2.0+ function. */
  40.    IF (alist_replyport)  THEN alist_replyport.ln.name := NIL   /* A little insurance... */
  41. ENDPROC
  42.  
  43.  
  44. /*
  45.  * For use in conjunction with barnacle_start()
  46.  *
  47.  * Make sure to call this only if alist_replyport is a valid port.
  48.  * Best way to do this is to call it only once, at the end of your program.
  49.  */
  50. PROC barnacle_end()
  51.    DEF tmp:PTR TO mn
  52.  
  53.    IF (alist_replyport)
  54.       /* Use my nice little routine to kill a port */
  55.       IF (alist_replyport.ln.name)  THEN RemPort (alist_replyport)
  56.       Forbid ()
  57.       WHILE (tmp := GetMsg (alist_replyport))
  58.          Remove (tmp)
  59.          IF (tmp.replyport)
  60.             Permit()
  61.             ReplyMsg (tmp)
  62.             Forbid()
  63.          ENDIF
  64.       ENDWHILE
  65.       DeleteMsgPort (alist_replyport)
  66.       Permit()
  67.    ENDIF
  68.  
  69.    alist_replyport := NIL
  70. ENDPROC
  71.  
  72.  
  73. /*
  74.  * Procedure for our barnacles to use... (:
  75.  *
  76.  * Note that 'arg' isn't in the same place it is in the OBJECT...
  77.  * Returns -1 (TRUE) if it can't send the message
  78.  *
  79.  * Note that 'rp' is required!  If we don't wait for the reply, and
  80.  *   deallocate the message, it won't get through.
  81.  * Also note that the 'rp' message list must be empty, or we'll
  82.  *   eat a waiting message and free up the message which was sent.
  83.  *
  84.  * Easiest way to use this func is to call barnacle_start() at the
  85.  *   start of your program, then pass in NIL as the 'rp', which will
  86.  *   cause it to use the allocated port.  Be sure to call barnacle_end()
  87.  *   before you quit though...
  88.  */
  89. PROC barnacle_put_msg (rp, cmd, arg=NIL, field1=NIL, field2=NIL)
  90.    DEF msg:PTR TO almsg, tmp, tmp2
  91.  
  92.    IF (rp = NIL)  THEN rp := alist_replyport
  93.  
  94.    NEW msg
  95.    msg.mn.replyport := rp
  96.    msg.mn.length := SIZEOF almsg
  97.    msg.command := cmd
  98.    msg.field1 := field1
  99.    msg.field2 := field2
  100.    msg.arg := arg
  101.  
  102.    Forbid ()
  103.    tmp := FindPort ('AList')
  104.    IF (tmp)
  105.       PutMsg (tmp, msg)
  106.    ENDIF
  107.    Permit ()
  108.  
  109.    tmp2 := NIL
  110.    IF (tmp)  THEN tmp2 := WaitPort (rp)
  111.    IF (tmp2)  THEN Remove (tmp2)
  112.  
  113.    END msg
  114. ENDPROC (tmp = FALSE)
  115.  
  116.  
  117.